﻿// -----------------------------------------------------------------------
// <copyright file="ExcelCellStage.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace DR5.SPExtend
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ExcelCellStage
    {
        private ExcelCol col;
        private int rowNumber;

        public ExcelCellStage(string cellCol, int rowNumber)
        {
            col = new ExcelCol(cellCol);
            rowNumber = rowNumber == 0 ? 1 : rowNumber;
            this.rowNumber = rowNumber;
        }

        public ExcelCellStage(int columnNumber, int rowNumber)
        {
            col = new ExcelCol(columnNumber);
            rowNumber = rowNumber == 0 ? 1 : rowNumber;
            this.rowNumber = rowNumber;
        }

        public string Current
        {
            get
            {
                return col.Value + rowNumber;
            }
        }

        public string Up
        {
            get
            {
                rowNumber--;
                return col.Value + rowNumber;
            }
        }

        public string UpBy(int value)
        {
            return col.Value + (rowNumber - value);
        }

        public string Down
        {
            get
            {
                rowNumber++;
                return col.Value + rowNumber;
            }
        }

        public string DownBy(int value)
        {
            return col.Value + (rowNumber + value);
        }

        public string Left
        {
            get
            {
                col--;
                return col.Value + rowNumber;
            }
        }

        public string LeftBy(int value)
        {
            return (col - value).Value + rowNumber;
        }

        public string Right
        {
            get
            {
                col++;
                return col.Value + rowNumber;
            }
        }

        public string RightBy(int value)
        {
            return (col + value).Value + rowNumber;
        }

        public string End
        {
            get
            {
                col.End();
                return col.Value + rowNumber;
            }
        }

        public string DownHome
        {
            get
            {
                rowNumber++;
                col.Home();
                return col.Value + rowNumber;
            }
        }

        public class ExcelCol
        {
            private string[] chars = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            protected internal int HigestValue = 0;

            public ExcelCol(string initValue)
            {
                CurrentValue = 0;
                initValue = initValue.ToUpper();
                if (initValue.Length == 2)
                {
                    string firstChar = initValue.Substring(0, 1);
                    string lastChar = initValue.Substring(1, 1);

                    int firstCharValue = 0;
                    foreach (string s in chars)
                    {
                        if (firstChar == s)
                        {
                            break;
                        }
                        firstCharValue++;
                    }

                    int lastCharValue = 0;
                    foreach (string s in chars)
                    {
                        if (lastChar == s)
                        {
                            break;
                        }
                        lastCharValue++;
                    }

                    firstCharValue = firstCharValue == 0 ? 1 : firstCharValue;
                    //lastCharValue = lastCharValue == 0 ? 1 : lastCharValue;

                    CurrentValue = firstCharValue * 26 + lastCharValue;
                }
                else
                {
                    foreach (string s in chars)
                    {
                        if (initValue == s)
                        {
                            break;
                        }
                        CurrentValue++;
                    }
                }
            }

            public ExcelCol(int columnNumber)
            {
                if (columnNumber == 0)
                {
                    CurrentValue = 0;
                }
                else
                {
                    CurrentValue = columnNumber - 1;
                }
            }

            private int _currentValue;
            private int CurrentValue
            {
                set
                {
                    _currentValue = value;
                    if (_currentValue >= HigestValue)
                    {
                        HigestValue = _currentValue;
                    }
                }
                get
                {
                    return _currentValue;
                }
            }

            protected internal string Value
            {
                get
                {
                    return GetValue();
                }
            }

            private string GetValue()
            {
                if (CurrentValue < 26)
                {
                    return chars[CurrentValue];
                }
                else
                {
                    int index = CurrentValue - 26;
                    int firstCharIndex = 0;
                    while (index >= 26)
                    {
                        index = index - 26;
                        firstCharIndex++;
                    }

                    return chars[firstCharIndex] + chars[index];
                }
            }

            public static ExcelCol operator +(ExcelCol col, int incrementBy)
            {
                col.CurrentValue = col.CurrentValue + incrementBy;
                return col;
            }

            public static ExcelCol operator -(ExcelCol col, int decrementBy)
            {
                col.CurrentValue = col.CurrentValue - decrementBy;
                return col;
            }

            public static ExcelCol operator ++(ExcelCol col)
            {
                return col + 1;
            }

            public static ExcelCol operator --(ExcelCol col)
            {
                if (col.CurrentValue == 0)
                {
                    return col;
                }
                else
                {
                    return col - 1;
                }
            }

            public void Home()
            {
                CurrentValue = 0;
            }

            public void End()
            {
                CurrentValue = HigestValue;
            }

            public int ColumnNumber()
            {
                return _currentValue;
            }
        }

        public string Home
        {
            get
            {
                col.Home();
                return col.Value + rowNumber;
            }
        }

        public int RowNumber
        {
            get
            {
                return rowNumber;
            }
        }

        public int ColumnNumber
        {
            get
            {
                return col.ColumnNumber()+1;
            }
        }
    }
}
